//
//  MAAdvViewController.m
//  MateAdv
//
//  Created by 茶 on 2023/9/7.
//

#import "MAAdvViewController.h"
#import "MAAdvView.h"
#import "MAAdvTipView.h"

@interface MAAdvViewController () <UINavigationControllerBackButtonHandlerProtocol>

@property (nonatomic, strong) MAAdvView *advView;
@property (nonatomic, strong) NSMutableArray <MAHomeRuleModel *>*contentArray;

@end

@implementation MAAdvViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initInterface];
}

- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture {
    if (_advView.isPlay) {
        MAWeakify(self);
        [LEEAlert alert].config
        .LeeTitle(@"提示")         // 添加一个标题 (默认样式)
        .LeeContent(@"是否退出当前游戏")        // 添加一个标题 (默认样式)
        .LeeAction(@"取消", ^{        //添加一个默认类型的Action (默认样式 字体颜色为蓝色)
        })
        .LeeCancelAction(@"确定", ^{    // 添加一个取消类型的Action (默认样式 alert中为粗体 actionsheet中为最下方独立)
            MAStrongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        })
        .LeeShow();
        return NO;
    }
    return YES;
}

- (void)initInterface {
    self.title = @"情侣飞行棋";
    self.view.backgroundColor = MAColor(@"#FF0071");
    [self.view addSubview:self.advView];
    [_advView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)setModel:(MAHomeModel *)model {
    _model = model;
    _contentArray = [model.ruleList copy];
}

- (void)showTipViewWithEnd:(BOOL)end index:(NSUInteger)index {
    MAAdvTipView *tipView = [MAAdvTipView new];
    tipView.title = @"获胜者可以要求失败者做任何一件事情，不允许拒绝！";
    tipView.becomeEnd = end;
    if (!end) {
        NSUInteger idx = 0;
        if (index > _contentArray.count) {
            idx = _contentArray.count - 1;
        } else {
            idx = index - 1;
        }
        tipView.title = _contentArray[idx].content;
    }
    MAWeakify(self);
    tipView.retryBlock = ^{
    };
    tipView.otherBlock = ^{
        MAStrongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    };
    
    [[UIWindow mainWindow] addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (MAAdvView *)advView {
    if (!_advView) {
        _advView = [MAAdvView new];
        MAWeakify(self);
        _advView.tipBlock = ^(NSUInteger index) {
            MAStrongify(self);
            [self showTipViewWithEnd:NO index:index];
        };
        _advView.endBlock = ^{
            MAStrongify(self);
            [self showTipViewWithEnd:YES index:0];
        };
    }
    return _advView;
}

@end
